package com.yuanchu.search.listener;

import com.yuanchu.search.service.RefreshGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.yuanchu.search.listener.MqGoodsConstants.*;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-03-11 17:35
 */
@Slf4j
@Component
public class GoodsListener {

    @Resource
    private RefreshGoodsService refreshGoodsService;



    /**
     * 监听商品新增或修改的业务
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = GOODS_INSERT_QUEUE),
            exchange = @Exchange(name = GOODS_EXCHANGE),
            key = {GOODS_INSERT_KEY}
    ))
    public void listenOrderInsertOrUpdate(String message){
        refreshGoodsService.refreshGoods(message);
    }

    /**
     * 监听商品删除的业务
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = GOODS_DELETE_QUEUE),
            exchange = @Exchange(name = GOODS_EXCHANGE),
            key = {GOODS_DELETE_KEY}
    ))
    public void listenOrderDelete(String message){
        refreshGoodsService.removeGoods(message);
    }

}
